<?php
Class Card extends Controller {
    function Card() {
        parent::Controller();
        $this->load->model('mod_card');
    }

//=========================this is function for loading page master page====================//

    function index() {
        if($this->check_session()){
        $this->load->view('master');
        }else{}
        redirect('admin/');
        exit();
    }

    //======================this is function for viewing manager page=====================//

    function manager() {
        if($this->check_session()){
        $data['getdata'] = $this->mod_card->select();
        $this->load->view('master', $data);
        $data['title'] = 'product manager';
        }else{
            redirect('admin/');
            exit();
        }
    }

    //=======================this is function for loading  cardfolder page=================//

    function cardfolder() {
        if($this->check_session()){
        $this->load->view('master');
        }else{
            redirect('admin/');
            exit();
        }
    }

    //===================this is function for adding  new card and validation of the form==================//

    function add() {
        if($this->check_session()){
        $data['title'] = 'this is the add new page';
        $this->form_validation->set_rules('txt_cardname', 'Card Name', 'required|trim|max_length[30]');
        $this->form_validation->set_rules('card_type', 'Card Type', 'required|trim|max_length[30]');
        $this->form_validation->set_rules('txt_k', 'Price khmer', 'required|numeric|trim|max_length[30]');
        $this->form_validation->set_rules('txt_v', 'Price volunteer', 'required|numeric|trim|max_length[30]');
        $this->form_validation->set_rules('txt_tcm', 'Price Other', 'required|numeric|trim|max_length[30]');
        if ($this->input->post('btn_submit')) {// if validate form is fale
            if ($this->form_validation->run() == false) {
                $this->load->view('master', $data);
            } else {// if validate form is true  and form submit
                $this->mod_card->add();
                $this->session->set_userdata('addcard_ms', '<br/><dl id="system-message"><dd class="message message"><ul><li>Add Successful!</li></ul></dd></dl>');
                redirect('card/manager');
            }
        } else {//if not submit loading page master
            $this->load->view('card/add');
        }
        }else{
            redirect('admin');
            exit();
        }
    }

    //============================Function to create new card type ===========================//

    function add_type() {
        if($this->check_session()){
        $this->form_validation->set_rules('type_name', 'Card type name', 'required|trim|max_length[30]');
        if ($this->input->post('btn_submit')) {
            if ($this->form_validation->run() == false) {
                
          $this->load->view('master');
                
            } else { 
               
                $this->mod_card->addtype();
                $this->session->set_userdata('sucessaddcard', '<br/><dl id="system-message"><dd class="message message"><ul><li>Add Card Type Successful!</li></ul></dd></dl>');
                redirect('card/manager_add_type');           
            }        
        }else {
            $this->load->view('card/add_type');
        }
        }else{
            redirect('admin/');
            exit();
        }
    }

    //=============Function to delete card =========================//

    function delete() {
        if($this->check_session()){
        $this->mod_card->delete();
        $this->session->set_userdata('delcard_ms','<br/><dl id="system-message"><dd class="message message"><ul><li>Deleted Successful!</li></ul></dd></dl>');
        redirect('card/manager');
        }else{
            redirect('admin/');
            exit();
        }
    }

    //========================this is function for editing card================//

    function edit() {
        if($this->check_session()){
        if ($this->input->post('btn_update')) {
            $this->mod_card->edit();
            $this->session->set_userdata('edicard_ms', '<br/><br/><br/><dl id="system-message"><dd class="message message"><ul><li>Edited Successful!</li></ul></dd></dl>');
            redirect('card/manager');
        }
        else
            $this->load->view('master');
        }else{
            redirect('admin/');
            exit();
        }
    }

    //====================this is function for add type=========================//
    
    function manager_add_type() {
        if($this->check_session()){
        if ($this->input->post('btn_submit')) {
            $data['card_type'] = $this->mod_card->add_car_type();
            $this->load->view('master', $data);
        }
        $this->load->view('master');
        }  else {
            redirect('admin/');
            exit();
        }
    }

    //=================this is function for delect add type====================//
    
    
    function delete_add_type() {
        if($this->check_session()){
        $this->mod_card->delete_add_type();
        $this->session->set_userdata('delcard_ms', '<br/><dl id="system-message"><dd class="message message"><ul><li>Deleted Successful!</li></ul></dd></dl>');
        redirect('card/manager_add_type');
    }else{
        redirect('admin/');
        exit();
    }
    }

    
    //================this is function for edit type card======================//
    
    function edit_type_card() {
        if($this->check_session()){
        if ($this->input->post('btn_update')) {
            $this->mod_card->edit_type_card();
            $this->session->set_userdata('delcard_ms', '<br/><dl id="system-message"><dd class="message message"><ul><li>Edit Successful!</li></ul></dd></dl>');
            redirect('card/manager_add_type/');
        }else
            $edit['select_card_type'] = $this->mod_card->select_edit();
        $this->load->view('master', $edit);
    }else{
        
        redirect('admin/');
        exit();
    }
    
    }
    
    
     
    //========this is function for using checking session=============
    
     public function check_session() {
        if ($this->session->userdata('use_username')) {
            return true;
        } else {
            return false;
        }
    }

}
